package com.example.demo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import java.util.jar.JarInputStream;

import static com.example.demo.MacUtil.getMacAddress;

/**
 * @创建人 Mr.Yan
 * @创建时间 2018/8/27
 * @描述:
 */
public class panel {
    public static int width = 540;
    public static int height = 480;

    public static  int x = 30;
    public static int y = 25;
    public static int w = 90;
    public static int h  = 30;

    public static int cx = 0;
    public static int cy = 25;
    public static void main(String[] args) {
        //新建一个JFrame对象frame，同时其标题栏为No Title
        JFrame frame=new JFrame("获取KEY");
        //原加密 文本框
        JLabel labeljqm = new JLabel("机器码:");
        JTextField jTextFieldjqm = new JTextField();


        //原加密 文本框
        JLabel label = new JLabel("String:");
        JTextField jTextField = new JTextField();

        //加密的 文本框
        JLabel labellock = new JLabel("StringLock:");
        JTextField jTextFieldlock = new JTextField();

        //原加密 文本框
        JLabel labeltype = new JLabel("加密前缀:");
        JTextField jTextFieldtype = new JTextField("flow");

        //原加密 文本框
        JLabel labelstrkey = new JLabel("自定义key:");
        JTextField jTextFieldstrkey = new JTextField("hmy");

        //加密 文本框
        JLabel labelout = new JLabel("M-KEY:");
        JTextField jTextFieldout = new JTextField();

        //加密 文本框
        JLabel labelauth = new JLabel("A-KEY:");
        JTextField jTextFieldauth = new JTextField();

        JButton button = new JButton("直接加密");
        JButton button2 = new JButton("解密加密");
        JButton button3 = new JButton("获取机器码");

        //新建一个JPanel面板panel，上面用来摆东西
        JPanel panel=new JPanel();
        panel.add(labeljqm);
        panel.add(jTextFieldjqm);

        //在panel上面摆上label
        panel.add(jTextField);
        panel.add(label);

        panel.add(labellock);
        panel.add(jTextFieldlock);

        panel.add(labeltype);
        panel.add(jTextFieldtype);

        panel.add(labelstrkey);
        panel.add(jTextFieldstrkey);

        panel.add(labelout);
        panel.add(jTextFieldout);

        panel.add(labelauth);
        panel.add(jTextFieldauth);

        panel.add(button);
        panel.add(button2);
        panel.add(button3);

        //设置panel的布局为任意null布局，这样下面的setBounds语句才能生效，并且label在这个面板的(125,75)位置，且大小为100x20px
        panel.setLayout(null);

        labeljqm.setBounds(x,y,w,h);
        jTextFieldjqm.setBounds(x+w+cx,y,2*w,h);

        label.setBounds(x,2*y+cy,w,h);
        jTextField.setBounds(x+w+cx,2*y+cy,4*w,h);

        labellock.setBounds(x,4*y+cy,w,h);
        jTextFieldlock.setBounds(x+w+cx,4*y+cy,4*w,h);


        labeltype.setBounds(x,6*y+cy,w,h);
        jTextFieldtype.setBounds(x+w+cx,6*y+cy,4*w,h);

        labelstrkey.setBounds(x,8*y+cy,w,h);
        jTextFieldstrkey.setBounds(x+w+cx,8*y+cy,4*w,h);


        labelout.setBounds(x,10*y+cy,w,h);
        jTextFieldout.setBounds(x+w+cx,10*y+cy,4*w,h);

        labelauth.setBounds(x,12*y+cy,w,h);
        jTextFieldauth.setBounds(x+w+cx,12*y+cy,4*w,h);

        button.setBounds(x,14*y+cy,2*w,h);
        button2.setBounds(x+3*w+cx,14*y+cy,2*w,h);
        button3.setBounds(x+w+cx+2*w,y,2*w,h);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str = jTextField.getText();
                //自定义追加加密前缀
                String type = jTextFieldtype.getText();
                //自定义密钥
                String key = jTextFieldstrkey.getText();
                try {
                    DesUtils des = new DesUtils(key);
                    String mkey = des.encrypt(str);
                    String akey = des.encrypt(type+str);
                    jTextFieldout.setText(mkey);
                    jTextFieldauth.setText(akey);
                } catch (Exception e1) {
                    jTextFieldout.setText("出现未知错误");
                    jTextFieldauth.setText("出现未知错误");
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String strlock = jTextFieldlock.getText();
                //自定义追加加密前缀
                String type = jTextFieldtype.getText();
                //自定义密钥
                String key = jTextFieldstrkey.getText();
                try {
                    DesUtils des = new DesUtils(key);
                    String  str = des.decrypt(strlock);
                    String mkey = des.encrypt(str);
                    String akey = des.encrypt(type+str);
                    jTextField.setText(str);
                    jTextFieldout.setText(mkey);
                    jTextFieldauth.setText(akey);
                } catch (Exception e1) {
                    jTextFieldout.setText("出现未知错误");
                    jTextFieldauth.setText("出现未知错误");
                }
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String jiqima  = getMacAddress();
                    jTextFieldjqm.setText(jiqima);
                } catch (Exception e1) {
                    jTextFieldjqm.setText("出现未知错误");
                }
            }
        });
        //在frame中添加panel
        frame.getContentPane().add(panel);
        //设置frame的大小为300x200，且可见默认是不可见的
        frame.setSize(width,height);
        frame.setVisible(true);
        //使右上角的关闭按钮生效，如果没有这句，点击右上角的关闭按钮只能关闭窗口，无法结束进程
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




    }
}
